﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LuaFramework;
/// <summary>
/// 模拟服务器后端逻辑
/// </summary>
public class SimulateServerCallBackData : MonoBehaviour {

    //后台的所有玩家
    private Dictionary<string, string> allUser = new Dictionary<string, string>();

	// Use this for initialization
	void Start () {
        for (int i = 10000; i < 10086; i++)
        {
            allUser.Add(i.ToString(), i.ToString());
        }
	    HeroManager.instance.Lv = 10;
	    HeroManager.instance.Exp = 100;
	    HeroManager.instance.FightValue = 100000;


        //监听客户端请求玩家名称
        EventDispatcher.AddEventListener<string>(EventKey.CSOnPlayerName, ReceiveRequestPlayerName);

        EventDispatcher.AddEventListener<string>(EventKey.CSOnSendPlayerNameToServer, ReceivePlayerCrateRoleName);//监听客户端发送的玩家名字创建成功的消息
        
        //监听客户端请求玩家信息
	    EventDispatcher.AddEventListener(EventKey.CSOnHeroInfo,ReceiveRequestHeroInfo);
	}
	
	// Update is called once per frame
	void Update () {
        if (Input.GetKeyDown(KeyCode.R)) {
            //把注册的玩家 加到所有玩家的字典中
            allUser.Add(LoginManger.instance.RegistAccountInfo.account, LoginManger.instance.RegistAccountInfo.password);

            //开始给客户端回数据
            LoginNetHandler.instance.OnReceiveServerBackRegistAccountResponse(1000, LoginManger.instance.RegistAccountInfo);

        }
        if (Input.GetKeyDown(KeyCode.L))
        {
            bool isLoginSuccess = false;

            if (allUser.ContainsKey(LoginManger.instance.Account)&&allUser[LoginManger.instance.Account]== LoginManger.instance.Password)
            {
                isLoginSuccess = true;
            }
            LoginNetHandler.instance.OnReceiveServerBackLoginAccountResponse(1003, isLoginSuccess);
        }

        if(Input.GetKeyDown(KeyCode.T))
        {
            EventDispatcher.AddEventListener<string>(EventKey.CSOnPlayerName, ReceiveRequestPlayerName);
        }

	    if (Input.GetKeyDown(KeyCode.K))
	    {
	        HeroManager.instance.Lv += 1;
	        HeroManager.instance.Exp += 100;
	        HeroManager.instance.FightValue += 1000;
	    }
    }

    //接收到请求玩家名称
    private void ReceiveRequestPlayerName(string data)
    {
        //准备数据（序列化数据），数据准备好之后，发送给客户端

        EventDispatcher.TriggerEvent<string>(EventKey.SCOnRequestPlayerName, HeroManager.instance.Name);
    }

    /// <summary>
    /// 接收客户端发来的消息
    /// </summary>
    /// <param name="data"></param>
    private void ReceivePlayerCrateRoleName(string data)
    {
        //服务器接收到客户端传来的玩家名称消息
        //1、自己重新判断该玩家名称是否合法  ---->>合法---存数据库             ----->>不合法---返回消息创建角色失败（弹出消息提示）
        //LoginNetHandler.instance.SentToServerCrateRoleNameSuccess(data);
        HeroManager.instance.OnReceiveRoleNameResponse(data);
    }

    //接受到客户端请求玩家的数据     需要把玩家数据整理后发送给客户端
    private void ReceiveRequestHeroInfo()
    {
        HeroData heroData = new HeroData();
        heroData.name = HeroManager.instance.Name;
        heroData.lv = HeroManager.instance.Lv;
        heroData.exp = HeroManager.instance.Exp;
        heroData.fightValue = HeroManager.instance.FightValue;
        //发送给客户端
        EventDispatcher.TriggerEvent<HeroData>(EventKey.SCOnHeroInfo, heroData);
    }

    void OnDestroy()
    {
        EventDispatcher.RemoveEventListener<string>(EventKey.CSOnPlayerName, ReceiveRequestPlayerName);

        EventDispatcher.RemoveEventListener<string>(EventKey.CSOnSendPlayerNameToServer, ReceivePlayerCrateRoleName);//监听客户端发送的玩家名字创建成功的消息

        EventDispatcher.RemoveEventListener(EventKey.CSOnHeroInfo, ReceiveRequestHeroInfo);
    }
}
